Create new Builder interface for creating CIDs.#53
Conversation
I'd leave
I don't care much either way. However, I'd slightly prefer remaining consistent with |
format.go
Outdated
| HashLen int // 0 (or -1 for compatibility) means the default length | ||
| } | ||
|
|
||
| func PrefixToFormat(p Prefix) Format { |
There was a problem hiding this comment.
Not sure if we really need this given that all Prefixs are Formats.
There was a problem hiding this comment.
The idea was that we might want to deprecate Prefix.Sum. This way we we can format MhLength from being -1, which is a source of confusion.
There was a problem hiding this comment.
We can just check that on Sum (after a deprecation period). That is, I'd like to move away from having MhLength == -1 (except for ID hashes, maybe). However, this is only an issue right now because the best way to create a CID is to construct a Prefix and then call Sum (and lazy programmers just set MhLength = -1).
Basically, Prefix.Sum isn't the problem (and is quite useful). It's Prefix.MhLength == -1.
There was a problem hiding this comment.
I would go one step further and not add an exception for ID hashes. That is a round trip from Prefix.Sum(data) and back to Prefix should create identical results. If you want to create an ID hash use the new FormatV1 struct.
There was a problem hiding this comment.
My point is that I really don't see why we can't just make Prefix implement Format (well, Builder now). Internally, it would just do exactly what the function above is doing and then call Sum. It just feels like adding this extra step isn't really necessary.
format.go
Outdated
| type FormatV1 struct { | ||
| Codec uint64 | ||
| HashFun uint64 | ||
| HashLen int // 0 (or -1 for compatibility) means the default length |
There was a problem hiding this comment.
I'd just say HashLen <= 0 means "default".
format.go
Outdated
| if mhLen == 0 { | ||
| mhLen = -1 | ||
| } | ||
| hash, err := mh.Sum(data, p.HashFun, mhLen) |
There was a problem hiding this comment.
Should check if we're using the ID hash and verify that the hash length is <= 0
There was a problem hiding this comment.
We already basically have this check in mh.Sum.
I find @Mr0grog any opinions on this, you where pretty vocal abort short names in ipfs/kubo#5056 |
|
@Stebalien I updated the depreciated notes so that only setting
|
Ha! I’m not very familiar with this package at all, so take my commentary with a grain of salt. Knowing nothing else, I’d agree: BUT, I am sympathetic to @Stebalien’s point on consistency with Either way, I think it would be helpful to clarify via comments that the field is intended to be a hash function from the (Sorry for the slow reply; just got back from a few days away.) |
Prefix includes information on existing CIDs. Format is for creating new CIDs. We currently use Prefix for both but that leads to some odd cases (e.g, the user wants to specify "just use the default hash length" but a valid prefix technically needs to explicitly specify the hash length). |
Ah, ok. Is this mainly so code that deals with it never has to check/modify the hash length the way metadata := cid.NewPrefixV1(cid.DagCBOR, mh.SHA2_256)
// or if Prefix-using functions could handle default length lookups when 0, or there was an accessor for hash length:
// metadata := cid.Prefix{Version: 1, Codec: cid.DagCBOR, MhType: mh.SHA2_256}
id := metadata.Sum(bytesOfContent)
// vs.
metadata := cid.FormatV1{Codec: cid.DagCBOR, HashFun: mh.SHA2_256}
id := metadta.Sum(bytesOfContent)…which didn’t feel like it was getting us much, but I also don’t know what "additional functionality" we are heading towards. If there’s a lot on the table, I can see how all the empty/optional fields a struct affords that Go’s lack of optional arguments doesn’t would be useful. Since these two structs are so similar, this definitely feels like it needs some documentation pointing the way (both text in the package overview saying you should use the Anyway, on the original point: all that does make me feel like having the names match is pretty valuable in this case (unless we might be comfortable changing |
So, there were two concrete issues:
Basically, Actually, @kevina, that reminds me of something. This version hardcodes the codec and doesn't provide a way to change it. That could be a problem for raw nodes etc. I'm thinking we may want something that takes the Codec in the |
|
Ah, thanks for this; it explains a good bit of background. Did I miss an obvious link somewhere here?
Oh! I don’t think I would have expected that from the name. |
Any suggestions? We've been through quite a few names but haven't found one we're completely satisfied with. |
|
Hmm… |
Your right. I added methods to do so and tested this now with |
|
I think For one thing I think renaming |
|
@Stebalien @Mr0grog after thinking about it I think |
|
Builder sounds like a great name! |
|
Sounds good to me :) |
Stebalien
left a comment
There was a problem hiding this comment.
Overall, I like this method. However, I have some concerns about PrefixToBuilder(p).Sum not behaving the same way as p.Sum.
Other than that, I'd like to move forward on this ASAP. Please bug me repeatedly on IRC whenever you're waiting for feedback.
| } | ||
| } | ||
|
|
||
| func (p Prefix) GetCodec() uint64 { |
There was a problem hiding this comment.
If Prefix is going to implement Builder, PrefixToBuilder(p).Sum(...) should be functionally equivalent to p.Sum(...). My solution would be to just put the logic in PrefixToBuilder into Prefix.Sum but the alternative is to just call PrefixToBuilder in Sum (actually, that may just be the better solution).
| return p.Codec | ||
| } | ||
|
|
||
| func (p Prefix) WithCodec(c uint64) Builder { |
There was a problem hiding this comment.
Should comment that this will auto-upgrade to CIDv1 if necessary. Actually, I wonder if this should return a prefix or just return a builder (V1/V0).
There was a problem hiding this comment.
Yes that is intentional. I don't see any reason why this should return a builder V1/V0.
There was a problem hiding this comment.
It was just a thought. I don't really see any benefit one way or the other.
|
@Stebalien I do have one question though do you want to eventually depreciate Prefix.Sum? |
|
|
|
I just removed PrefixToBuilder for now. It doesn't have a useful purpose as of now and just serves to cause confusion. |
|
Okay. I clean up the code to remove all the controversial parts so this should be ready for review. Before we merge this it needs to be tested with |
No. That is, I'd just treat Having the extra "convert to a builder" step just feels like, well, an extra step. It'll either introduce two ways to do the same thing, two things that look like they should do the same thing but don't, or a lot of code churn if we deprecate |
I don't necessary agree with this, but that is something to revisit later. |
It may not make sense in all cases however, in those where it doesn't, I expect we'll want something fancier than a simple Basically, I agree that |
|
Okay. I completed the integration testing so this should be good to go. Let's get #61 is also before we |
This is my proposed alternative to #52
My making
Formatan interface we can easily extend it to provide additional functionality outside of thego-cidpackage. In addition the currentPrefiximplemented theFormatinterface so to take advantage of the added functional all we need to is is change the signature of functions that take acid.Prefixtocid.Format.I specifically did not add support for automatically creating identity hashes as that can be added later, and possible outside of
go-cid. The code could look something like:At some point in the future we can remove NewPrefixV0, NewPrefixV1 and Prefix.Sum. We should also consider making the MhLength field an unsigned integer to disallow -1 as a value.
Some additional nodes: I find the field names
MhTypeandMhLengthcrypt and thinkHashFunandHashLenare better and easier to understand names so I used those in this structure. If we are dead set onMhTypeandMhLengthI can change it back.